/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Helpers;

import Hibernate.HibernateUtil;
import Entities.Resultaten;
import java.util.List;
import org.hibernate.*;
import java.sql.Date;
import java.util.Calendar;
/**
 *
 * @author Erhan
 */
public class ResultatenHelper {

    Session session = null;

    /**
     *
     */
    public ResultatenHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    //Ophalen alles
    /**
     *
     * @return
     */
    public List<Resultaten> getresultaten() {
        List<Resultaten> resultatenList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Resultaten");
            resultatenList = (List<Resultaten>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultatenList;
    }

    //Ophalen van 1
    /**
     *
     * @param id
     * @return
     */
    public Resultaten getResultaat(int id) {
        Resultaten result = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Resultaten where id=" + id);
            result = (Resultaten) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    // Wegschrijven
    /**
     *
     * @param resultaat
     */
    public void persistResultaat(Resultaten resultaat) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.persist(resultaat);
        tx.commit();
    }

    // Verwijderen
    /**
     *
     * @param resultaat
     */
    public void deleteResultaat(Resultaten resultaat) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.delete(resultaat);
        tx.commit();
    }

    // Updaten
    /**
     *
     * @param resultaat
     */
    public void updateResultaat(Resultaten resultaat) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.update(resultaat);
        tx.commit();
    }

    /**
     *
     * @param cijfer
     * @param tid
     * @param pid
     * @param cid
     */
    public void slaResultaatop(byte cijfer, int tid, int pid, int cid){
        Resultaten resultaat = new Resultaten();
        KlassenHelper khelper = new KlassenHelper();
        resultaat.setCijfer(cijfer);
        resultaat.setToetsId(tid);
        Calendar c = java.util.Calendar.getInstance();
        int maand = c.get(Calendar.MONTH);
        int dag = c.get(Calendar.DAY_OF_MONTH);
        int jaar = c.get(Calendar.YEAR);
        String datum = jaar + "-" + maand + "-" + dag;
        Date d = Date.valueOf(datum);
        resultaat.setStartDatum(d);
        int kid = khelper.klasBijCursus(pid, cid);
        resultaat.setKlassen(khelper.getKlas(kid));
        persistResultaat(resultaat);
    }
}